<?php
/*
 * Created on Feb 20, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
include('../includes/session.php');
include('../models/info_model.php');

class GeneralInfoController{
	
	function GeneralInfoController(){
		global $session;
		if(isset($_POST['infoedit'])){
        	$this->genInfoEdit();
     	}else if(isset($_POST['infoadd'])){
        	$this->genInfoAdd();
     	}else if(isset($_POST['infowebsite'])){
        	$this->updateInfoWebsite();
     	}else if(isset($_POST['banneradd'])){			
        	$this->bannerAdd();
     	}else if(isset($_POST['banneredit'])){
        	$this->bannerEdit();
     	}else{       		
        	header("Location: ".$session->referrer);
       	}
	}
	
	function bannerAdd(){
		global $session, $form;
		$resultValidation = 0;
		$arrayData = array();
		$arrayData['url'] = $_POST['url'];
		$arrayData['sortNo'] = $_POST['sortNo'];		
		$arrayData['createDate'] = $_POST['createDate'];		
		
		$resultValidation = $this->checkBannerValidation(
										null,
										$arrayData,
										$_FILES['image']);
		/* Add Successful */
		if($resultValidation == 0){
	        $_SESSION['success'] = true;			
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else if($resultValidation == 1){
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
	    }
	    /* Add attempt failed */
	    else if($resultValidation == 2){	        
	        $_SESSION['success'] = false;			
	        header("Location: ".$session->referrer);
		}
	}
	
	function bannerEdit(){
		global $session, $form;		
		$resultValidation = 0;
		$arrayData = array();
		$arrayData['url'] = $_POST['url'];
		$arrayData['sortNo'] = $_POST['sortNo'];		
		$arrayData['createDate'] = $_POST['createDate'];
		
		$resultValidation = $this->checkBannerValidation(
										$_POST['SelectedBanner'],
										$arrayData,
										$_FILES['image']);
										
		/* Account edit successful */
	    if($resultValidation == 0){
	    	$_SESSION['edit'] = true;
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else{
	        $_SESSION['edit'] = false;
	        header("Location: ".$session->referrer);
		}
	}
	
	function genInfoEdit(){
		global $session, $form;
		
		$resultValidation = $this->checkValidation(
										$_POST['SelectedIfdc'],
										$_POST['SelectedIfd'],
										$_POST['title'],
										$_FILES['image'],
										$_POST['header'],
										stripslashes($_POST['content']),
										$_POST['sortNo'],
										$_POST['isTop'],
										null);
		/* Info edit successful */
	    if($resultValidation == 0){
	    	$_SESSION['edit'] = true;
			$_SESSION['editname'] = $_POST['title'];
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else{
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
		}
		
	}
	
	function genInfoAdd(){
		global $session, $form;
		
	    $resultValidation = $this->checkValidation(
										null,
										$_POST['SelectedIfd'],
										$_POST['title'],
										$_FILES['image'],
										$_POST['header'],
										stripslashes($_POST['content']),
										$_POST['sortNo'],
										$_POST['isTop'],
										$_POST['createDate']);
	    	
		/* Add Successful */
		if($resultValidation == 0){
	    	$_SESSION['successname'] = $_POST['title'];
	        $_SESSION['success'] = true;
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else if($resultValidation == 1){
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
	    }
	    /* Add attempt failed */
	    else if($resultValidation == 2){
	        $_SESSION['successname'] = $_POST['title'];
	        $_SESSION['success'] = false;
	        header("Location: ".$session->referrer);
		}
	}
	
	function checkValidation($id = null, $type = null, $title, $image, $header, $content, $sortno, $istop, $createDate){
		global $form, $infoModel;
		/* Username error checking */
      	$field = "title";  //Use field name for title
	    if(!$title || strlen($title = trim($title)) == 0){
	    	$form->setError($field, prnMsg(_('Nhập tiêu đề'),'error'));
	    }
		
		$field = "header";  //Use field name for header
	    if(!$header || strlen($header = trim($header)) == 0){
	    	$form->setError($field, prnMsg(_('Nhập hiển thị tóm tắt'),'error'));
	    }
				
		if(!$sortno || strlen($sortno = trim($sortno)) == 0){
	    	$sortno = 0;
	    }
		
		if(!$istop || strlen($istop = trim($istop)) == 0){
	    	$istop = 0;
	    }
		
		$field = "image";  //Use field name for image
		$extention = "";
		if (isset($image) AND $image['name'] !='') {
			$extention = strtoupper(substr(trim($image['name']),strlen($image['name'])-3));
			 //But check for the worst
			if ($extention != 'JPG' && $extention != 'GIF' && $extention != 'PNG'){
				$form->setError($field, prnMsg(_('Chỉ hỗ trợ file ảnh dạng .jpg, .gif, .png'),'error'));
			} elseif ( $image['size'] > ($_SESSION['MaxFileUpload']*1024)) { //File Size Check
				$form->setError($field, prnMsg(_('Kích thước file vượt kích thước cho phép ('.$_SESSION['MaxFileUpload'].')'),'error'));				
			} elseif ( $image['type'] == "text/plain" ) {  //File Type Check
				$form->setError($field, prnMsg(_('Chỉ chấp nhận file dạng ảnh'),'error'));				
			}		
		}

		/* Errors exist, have user correct them */
	    if($form->num_errors > 0){
	    	return 1;  //Errors with form
	    }/* No errors, add the new account to the */
	    else{
	    	if(!isset($id)){
	    		$result = $infoModel->addNewInfo($type, $title, $header, $content, $sortno, $istop, $createDate);
		        if($result == 0 && isset($image) AND $image['name'] != '' ){
		        	return $this->updateInfoImage($_SESSION['LastInsertId'], $image);		        	
				}
				return $result;
			}else{
				$result = $infoModel->editInfo($id, $type, $title, $header, $content, $sortno, $istop);
				if($result == 0){
					return $this->updateInfoImage($id, $image);		        	
				}
				return $result;
			}
	   	}
	}
	
	function checkBannerValidation($id = null, $arrayData = null, $image){
		global $form, $infoModel;
		if(!$arrayData['sortNo'] || strlen($arrayData['sortNo'] = trim($arrayData['sortNo'])) == 0){
	    	$arrayData['sortNo'] = 0;
	    }
		
		/* Errors image checking */
		$field = "image";  //Use field name for image
		$extention = "";
		if (isset($image) AND $image['name'] !='') {
			$extention = strtoupper(substr(trim($image['name']),strlen($image['name'])-3));
			 //But check for the worst
			if ($extention != 'JPG' && $extention != 'GIF' && $extention != 'PNG'){
				$form->setError($field, prnMsg(_('Chỉ hỗ trợ file ảnh dạng .jpg, .gif, .png'),'error'));
			} elseif ( $image['size'] > ($_SESSION['MaxFileUpload']*1024)) { //File Size Check
				$form->setError($field, prnMsg(_('Kích thước file vượt kích thước cho phép ('.$_SESSION['MaxFileUpload'].')'),'error'));				
			} elseif ( $image['type'] == "text/plain" ) {  //File Type Check
				$form->setError($field, prnMsg(_('Chỉ chấp nhận file dạng ảnh'),'error'));				
			}		
		}else if($id == null){
			$form->setError($field, prnMsg(_('Chưa chọn ảnh để thêm vào'),'error'));
		}
		
		/* Errors exist, have user correct them */
	    if($form->num_errors > 0){
	    	return 1;  //Errors with form
	    }/* No errors, add the new account to the */
	    else{
	    	if(!isset($id)){				
		        $result = $infoModel->addNewBanner($arrayData);				
				if($result == 0 && isset($image) AND $image['name'] != '' ){
		        	return $this->updateBannerImage($_SESSION['LastInsertId'], $image);		        	
				}
				return $result;
			}else{				
				$result = $infoModel->editBanner($id, $arrayData);
				if($result == 0){
					return $this->updateBannerImage($id, $image);		        	
				}
				return $result;
			}
	   	}
	}
	
	function updateInfoImage($id = null, $image = null){
		global $infoModel, $form;
		if(isset($image) AND $image['name'] != '' ){
		   	$extention = strtoupper(substr(trim($image['name']),strlen($image['name'])-3));
			$filename = $_SESSION['PathUploadContent'].$id.'.'.$extention;
			$infoModel->updateImageField($id, $id.'.'.$extention);		        		        	
		   	$resultUpload  =  move_uploaded_file($image['tmp_name'], $filename);
			if(!$resultUpload){
				$field = "image";
				$form->setError($field, prnMsg(_('Quá trình upload file bị lỗi.'),'error'));
				return 2;		
			}	
		}
		return 0;
	}
	
	function updateBannerImage($id = null, $image = null){
		global $infoModel, $form;
		if(isset($image) AND $image['name'] != '' ){
		   	$extention = strtoupper(substr(trim($image['name']),strlen($image['name'])-3));
			$filename = $_SESSION['PathUploadBanner'].$id.'.'.$extention;
			$infoModel->updateBannerImageField($id, $id.'.'.$extention);		        		        	
		   	$resultUpload  =  move_uploaded_file($image['tmp_name'], $filename);
			if(!$resultUpload){
				$field = "image";
				$form->setError($field, prnMsg(_('Quá trình upload file bị lỗi.'),'error'));
				return 2;		
			}	
		}
		return 0;
	}
	
	function updateInfoWebsite(){
		global $session, $infoModel;
		$result = 0;
		$arrayData = array();
		$arrayData['company'] = $_POST['company'];
		$arrayData['copyright'] = $_POST['copyright'];
		$arrayData['address'] = $_POST['address'];
		$arrayData['phone'] = $_POST['phone'];
		$arrayData['fax'] = $_POST['fax'];
		$arrayData['website'] = $_POST['website'];
		$arrayData['email'] = $_POST['email'];

		$result = $infoModel->updateInfoWebsite(1, $arrayData);
		/* Info edit successful */
	    if($result == 0){
	    	$_SESSION['edit'] = true;       
	    }
	    /* Error found with form */
	    else{
	        $_SESSION['edit'] = false;	        
		}
		header("Location: ".$session->referrer);
	}
};

/* Create userinfo controller object */
$infoController = new GeneralInfoController;
?>
